package com.xxl.job.executor.core.utils;

import org.apache.commons.lang.StringUtils;

import java.io.File;

public class ScanFiles {

    public static void foundFile() {
        ConfigUtil.getInstance();
        String srcName = ConfigUtil.SRCFILE;
        if (!isExist(srcName)) {
            System.out.println("目录文件--" + srcName + "不存在！");
        } else {
            System.out.println("正在扫描目录--" + srcName);
            scanfile(srcName,null);
        }
    }

    //dirName: CTL DOWNLOAD UPLOAD
    private static void scanfile(String srcfilepath,String dirName) {
        File root = new File(srcfilepath);
        File[] file = root.listFiles();
        for (File f : file) {
            if (f.isFile()){
                if (StringUtils.isBlank(dirName)){
                    System.out.println("当前文件--" + f.getName() + "不属于系统所属的目录！");
                }else {
                    //上传到ftp
                    System.out.println("当前正在遍历文件--"+f.getName());
                    UploadFiles.upload(getForwordDir(f.getParent()),f);
                }
            }else if (f.isDirectory()){
                System.out.println("当前正在遍历目录--" + f.getAbsolutePath());
                scanfile(f.toString(),f.getAbsolutePath());
            }
        }
    }

    public static boolean isExist(String filePath) {
        boolean exists = true;
        File file = new File(filePath);
        if (!file.exists()) {
            exists = false;
        }
        return exists;
    }

    private static String getForwordDir(String path){
        String forwordDir = path.substring(path.lastIndexOf("\\") + 1);
        return forwordDir;
    }
}
